<?php

    /**
    * Put all _GET variables in array to easier accessing them later
    *
    * @return array()
    */
    function extractGet()
    {
        $get = array();
        foreach($_GET as $key => $val) {
          $get[$key] = $val;
        }
        return $get;
    }


    /**
    * Put all _POST variables in array to easier accessing them later
    *
    * @return array()
    */
    function extractPost()
    {
        $post = array();
        foreach($_POST as $key => $val) {
          $post[$key] = $val;
        }
        return $post;
    }

    /**
     * Helper to quicker send error messages
     *
     * @param string $errorMessage
     * @param array $params
     * @return string
     */
    function jsonError($errorMessage, Array $params = []) {
        $result["success"] = 'false';
        $result["reason"] = vsprintf($errorMessage, $params);
        return json_encode($result);
    }
    
    function jsonSuccess($successParam, $successContent) {
        $result["success"] = 'true';
        $result[$successParam] = $successContent;
        return json_encode($result);
    }
    
    function jsonSuccessDie($successParam, $successContent) {
        echo jsonSuccess($successParam, $successContent);
        die();
    }
    
    function jsonErrorDie($errorMessage, Array $params = []){
        echo jsonError($errorMessage, $params);
        die();
    }
    
    // NOTE: Delay is in seconds!!!!
    function getDateWithDelay($delay){
        $todayDate = date("Y-m-d g:i a");// current date
        $currentTime = time($todayDate); //Change date into time


        //echo "<br>".$currentTime;
        //Add one hour equavelent seconds 60*60

        return date("Y-m-d H:i:s",($currentTime + $delay));

    }
    
    function getCurrentDate(){
        return date("Y-m-d g:i a");// current date
    }
    
    // Lock time = 5 min
    function isUserLocked($time){
        if(is_null($time)){
            return false;
        }
        $currentTime = time(getCurrentDate());
        $fiveMins = 60*5;
        // User is locked
        if($currentTime < ($time + $fiveMins)){
            return true;
        }
        else{
            return false;
        }
    }

    function getRoleName($role_id) {
        $array = [0 => 'admin', 1=>'kanbanmaster', 2 => 'Product Owner', 3=> 'Developer'];
        return $array[$role_id];
    }
    
    function getPossibleUserRoles($userId){
        // Add all possible roles.
         $roleIds = R::getAll("SELECT id FROM role WHERE id IN (SELECT role_id FROM role_user WHERE user_id = :user_id)", [':user_id' => $userId]);
         
         $idArray = array();
         foreach ($roleIds as $key => $value) {
             $idArray[] = $value["id"];
         }
         return $idArray;
    }

    function deactivetUserGroup($user_id, $group){
        $nonActive = json_decode($group->nonActiveUsers);
        array_push($nonActive, $user_id);
        return json_encode($nonActive);
    }

    function activateUserGroup($user_id, $group){
        $nonActive = json_decode($group->nonActiveUsers);
        $key = array_search($user_id, $nonActive);
        unset($nonActive[$key]);
        return json_encode(array_values($nonActive));
    }

    function addDeveloperGroup($user_id, $group){
        $devs = json_decode($group->developers);
        array_push($devs, $user_id);
        return json_encode(array_unique($devs));
    }

    function isInBeans($id, $beans){
        foreach($beans as $bean){
            if ($id == $bean->id) return true;
        }
        return false;
    }